﻿Imports System.Data.SqlClient

Public Class frmStoreAct

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Me.Close()
            With frmStore
                .bindData("")
                .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                .TopLevel = False
                .Dock = DockStyle.Fill
                frmMain.pnBody.Controls.Add(frmStore)
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString())
        End Try
    End Sub

    Private Shared Function validstore(ByVal storename As String, ByVal storeid As String)
        Try
            Dim storehave = dbconnection.ExecuteScalar("select * from Store where StoreName=N'" + storename + "' and StoreId<>'" + storeid + "';")
            Return storehave
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
            Return False
        End Try
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (validstore(txtStoreName.Text, lblStoreId.Text) IsNot Nothing) Then
            MessageBox.Show("Errors: Store Already Exists Please Try With Other...!")
            txtStoreName.Focus()
            Exit Sub
        End If
        If txtStoreName.Text = "" Then
            MessageBox.Show("Errors: Store name require...!")
            Exit Sub
        End If
        If lblStoreId.Text = "" Then
            Try
                Dim cmdadd As SqlCommand
                cmdadd = dbconnection.Connection.CreateCommand
                With cmdadd
                    .CommandType = CommandType.Text
                    .CommandText = "insert into Store (StoreName, Active) values (@storename, '1');"
                    .Parameters.Add("@storename", SqlDbType.NVarChar).Value = txtStoreName.Text.Trim()
                    .ExecuteNonQuery()
                End With
                MessageBox.Show("Record Added")
                Me.Close()
                With frmStore
                    .bindData("")
                    .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                    .TopLevel = False
                    .Dock = DockStyle.Fill
                    frmMain.pnBody.Controls.Add(frmStore)
                    .Show()
                End With
            Catch ex As Exception
                MessageBox.Show("Errors " + ex.ToString)
            End Try
        Else
            Try
                Dim cmdupdate As SqlCommand
                cmdupdate = dbconnection.Connection.CreateCommand
                With cmdupdate
                    .CommandType = CommandType.Text
                    .CommandText = "update Store set StoreName=@storename where StoreId=@storeid;"
                    .Parameters.Add("@storename", SqlDbType.NVarChar).Value = txtStoreName.Text.Trim()
                    .Parameters.Add("@storeid", SqlDbType.NVarChar).Value = lblStoreId.Text.Trim()
                    .ExecuteNonQuery()
                End With
                MessageBox.Show("Record Updated")
                Me.Close()
                With frmStore
                    .bindData("")
                    .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                    .TopLevel = False
                    .Dock = DockStyle.Fill
                    frmMain.pnBody.Controls.Add(frmStore)
                    .Show()
                End With
            Catch ex As Exception
                MessageBox.Show("Errors " + ex.ToString)
            End Try
        End If
    End Sub
End Class